App.controller("taskLookCtr",function($scope, $rootScope, $location, $http, $compile, $stateParams, $timeout, path, publicService) {
	var params = JSON.parse($stateParams.params);
	$scope.search ={};
	$scope.form = {};
	var task_data = [
	                 {
	                 	value: "",
	      				text: "请选择"
	                  },
	                  {
	                 	value: 1,
	      				text: "未开始"
	                  },
	                  {
	                 	value: 2,
	      				text: "进行中"
	                  },
	                  {
	                  	value: 3,
	       				text: "未完成"
	                   },
	                  {
	                 	value: 4,
	      				text: "已完成"
	                  }
	             ];
	var  taskDatas =[
	                 {
	                 	value: "",
	      				text: "请选择"
	                  },
	                  {
	                 	value: 1,
	      				text: "未开始"
	                  },
	                  {
	                 	value: 2,
	      				text: "进行中"
	                  },
	                  {
	                 	value: 3,
	      				text: "已结束"
	                  },
	                  {
	                 	value: 4,
	      				text: "已暂停"
	                  }
	             ]
	//任务状态
	$("#task_state").kendoDropDownList({
		dataTextField: "text",
	    dataValueField: "value",
		dataSource: taskDatas
	});	
	//任务完成情况
	$("#finish_condition").kendoDropDownList({
		dataTextField: "text",
	    dataValueField: "value",
		dataSource:task_data 
	});
	
	$scope.gridOptions = {
			dataSource : {
				transport : {
					read : {
						url : path+"/server/selectUerManagerTask",
						type : 'POST',
						dataType : "json",
						contentType : "application/json"
					},
					parameterMap : function(options, operation) {
						if (operation == "read") {
							var parameter = {
								page : options.page, //当前页
								pageSize : options.pageSize,
								param:{
									user_manager_id:params.brandId,  //导购id
									state:$('#task_state').val(),  //任务状态
									completion: $('#finish_condition').val(),//完成任务
									set_start_time:$('#start_time').val(),// 开始时间
									set_end_time:$('#end_time').val(),// 结束时间
									activity_name:$scope.search.activity_name// 活动名称
									
								}
							};
							console.log('查询参数',parameter);
							return kendo.stringify(parameter);
						}
					},
				},
				pageSize : 10,
				serverPaging : true,
				serverSorting: true,
				schema : {
					data : function(d) {
						return d.result.data;
					},
					total : function(d) {
						return d.result.total; //总条数
					}
				},
			},
			pageable : {  //分页
				pageSizes: [10, 20, 50, 100],  //每页显示记录数
			  },  
			sortable: {   //排序
				//allowUnsort: false,  //允许无序
			    mode: "multiple"     //排序模式：single，multiple
			  },
			editable: false,  //true：单个编辑；popup：弹出新页面编辑
			dataBound: function () {   //序号
		        var rows = this.items();
		        var page = this.pager.page() - 1;
		        var pagesize = this.pager.pageSize();
		        $(rows).each(function () {
		            var index = $(this).index() + 1 + page * pagesize;
		            var rowLabel = $(this).find(".row-number");
		            $(rowLabel).html(index);
		        });
		    },
			columns : [
					{    
						title : "序号",
						field : "id", 
						width : "50px",
						template:function(){
						 return	"<span class='row-number subTitle'></span>";
						}
					},
					{
						field : "",         
						title : "创建日期",     
						width : "100px",
						template:function(e){
							return publicService.switchTime(e.set_time,'dateTime');
						}
					},
					{
						field : "",         
						title : "任务起止日期",     
						width : "180px",
						template: function(e){
						
							return publicService.switchTime(e.task_starttime,'dateTime')+'--'+ publicService.switchTime(e.task_endtime,'dateTime')
						}
					},
					{
						field : "activity_name",          
						title : "活动名称",     
						width : "100px"
					},
					{
						field : "completion",         
						title : "完成情况",     
						width : "100px",
						values: task_data
					},
					{
						field : "state",         
						title : "任务状态",     
						width : "100px",
						values: taskDatas
					},
					{
						field : "",         
						title : "操作",     
						width : "100px",
						hidden:$scope.storeshow,
						template: function(e){
					    	var html = '';
					    	
					    	if(userInfo.user_level ==3){
					    		html += '<div class="operation" ng-click="lookProduct('+e.id+',1)">查看详情</div>';
					    	}else if(userInfo.user_level ==2){
					    		html += '<div class="operation" ng-click="lookProduct('+e.id+',1)">查看详情</div>';
					    	}else if(userInfo.user_level ==1){
					    		if(e.state ==0){
					    			html += '<div class="operation splitButtonTwoc" ng-click="lookProduct('+e.id+',0)">编辑</div>'
					    				+'<div class="operation splitButtonTwoc" ng-click="lookProduct('+e.id+',1)">查看详情</div>';
					    		}else{
					    			html += '<div class="operation" ng-click="lookProduct('+e.id+',1)">查看详情</div>';
					    		}
					    	}else{
					    		html += '<div class="operation" ng-click="lookProduct('+e.id+',1)">查看详情</div>';
					    	}
					    	
					    	return html;
					    },
					}
			]
	};
	//查询
	$scope.search = function(){
		$scope.grid.dataSource.page(1);
//		$scope.orderWindow.center().open();   //打开弹框
	}
	//查看详情1（编辑0）
	$scope.lookProduct = function(id,state){

		$http({
			   url: path+'/server/selectUerManagerTaskDetails/'+id+'/'+params.brandId,
			   method: 'GET'
		}).success(function(data){ 
			var resultData = data.result;
			if(data.code==0){  //成功
				$scope.form.activity_name = resultData.activity_name;  //活动名称
				$scope.form.task_starttime = publicService.switchTime(resultData.task_starttime,'dateTime');   //开始时间
				$scope.form.task_endtime =  publicService.switchTime(resultData.task_endtime,'dateTime');  //结束时间
				
				$scope.form.activity_num = Number(resultData.activity_num);  //活动成单量
				$scope.form.invite_activity_num = Number(resultData.invite_activity_num);  
				$scope.form.sign_rate = Number(resultData.sign_rate);  //签到率
				$scope.form.invite_sign_rate =  Number(resultData.invite_sign_rate); 
				
				$scope.taskWindow.center().open();   //打开弹框
				
				if(state == 0){  //编辑
					$("input").removeAttr("disabled");//让所有的input变为只读
					$scope.submits = true;  //确定提交按钮显示
				}else if(state == 1){  //查看详情
					$('.hiddenInput').attr("disabled","true");//让所有的input变为只读
					$scope.submits = false;  //确定提交按钮不显示
				}
			}else if(data.code==1){  //失败
				
			}
		}).error(function(data){
			alert(data);   
		})
	}
	//提交
	$scope.submitsData = function(){
		if(!$scope.form.activity_name){
			alert("请输入活动名称");
			return;
		}
		if(!$scope.form.activity_num){
			alert("请输入活动成绩单");
			return;
		}
		if(!$scope.form.customer_num){
			alert("请输入邀请客户人数");
			return;
		}
		
		$http({
			   url: path+'/server/updateUerManagerTask',
			   method: 'POST',
			   data:angular.toJson($scope.form)
		}).success(function(data){ 
			if(data.code==0){  //成功
				resultData = data.result;
			}else if(data.code==1){  //失败
				console.log(data);
			}
		}).error(function(data){
			alert(data);   
		})
	}
//	publicService.initActivityState("task_state");   //任务状态
	publicService.initDateTime("start_time");   //开始时间
	publicService.initDateTime("end_time");   //结束时间
});